import { message } from 'antd';
import { getWindowSelectList, queryListingData, queryWindow } from './services';
// @ts-ignore
import { HttpCode } from '#/utils/contacts';
import type { ApiResponse, Payload, State } from './components/type';

export default {
  namespace: 'ListingTransactionDeclaration',
  state: {
    list: [], // 窗口数据
    windowNameLength: 10, // 窗口名称长度(默认10)
    stationIdList: [], // 交易中心
    cardListData: [], // 卡片数据
    titleInfo: {}, // 标题信息
    windowConfigs: {}, // 窗口配置
    configLoading: false,
  } as State,

  // 更新状态
  reducers: {
    getList(state: State, { payload }: Payload) {
      return {
        ...state,
        list: payload,
      };
    },
    getWindowNameLength(state: State, { payload }: Payload) {
      return {
        ...state,
        windowNameLength: payload,
      };
    },
    getStationIdList(state: State, { payload }: Payload) {
      return {
        ...state,
        stationIdList: payload,
      };
    },
    getCardListData(state: State, { payload }: Payload) {
      return {
        ...state,
        cardListData: payload,
      };
    },
    getTitleInfo(state: State, { payload }: Payload) {
      return {
        ...state,
        titleInfo: payload,
      };
    },
    getConfigLoading(state: State, { payload }: Payload) {
      return {
        ...state,
        configLoading: payload,
      };
    },
    getWindowConfigs(state: State, { payload }: Payload) {
      return {
        ...state,
        windowConfigs: payload,
      };
    },
  },

  effects: {
    *fetchWindowSelectList(
      { payload }: Payload,
      { call, put }: { call: any; put: any },
    ): Generator<Promise<ApiResponse>, void, unknown> {
      try {
        const res = (yield call(getWindowSelectList, payload)) as ApiResponse;
        if (res.statusCode === HttpCode.SUCCESS) {
          yield put({
            type: 'getList',
            payload: res.data.windowSelectList,
          });
          yield put({
            type: 'getWindowNameLength',
            payload: res.data.windowNameLength,
          });
        } else {
          message.error('查询窗口数据失败');
        }
      } finally {
      }
    },

    *fetchData(
      { payload }: Payload,
      { call, put }: { call: any; put: any },
    ): Generator<Promise<ApiResponse>, void, unknown> {
      try {
        const params = {
          marketId: payload.marketId,
          dynamicFields: {
            stationIds: payload.stationIds,
            windowId: payload.windowId,
          },
        };
        const { data, statusCode } = (yield call(queryListingData, params)) as ApiResponse;
        if (statusCode === HttpCode.SUCCESS) {
          if (data) {
            yield put({
              type: 'getCardListData',
              payload: data.dataList,
            });
            yield put({
              type: 'getTitleInfo',
              payload: data.title,
            });
          }
        } else {
          message.error('查询挂牌数据失败');
        }
      } finally {
      }
    },
    *fetchQueryWindow(
      { payload }: Payload,
      { call, put }: { call: any; put: any },
    ): Generator<Promise<ApiResponse>, void, unknown> {
      try {
        yield put({
          type: 'getConfigLoading',
          payload: true,
        });
        const { data, statusCode } = (yield call(queryWindow, {
          marketId: 'GXCSG',
          windowId: payload,
        })) as ApiResponse;
        if (statusCode === HttpCode.SUCCESS) {
          if (data) {
            yield put({
              type: 'getWindowConfigs',
              payload: data,
            });
            yield put({
              type: 'getConfigLoading',
              payload: false,
            });
          }
        } else {
          message.error('获取窗口配置失败');
          yield put({
            type: 'getConfigLoading',
            payload: false,
          });
        }
      } catch (error) {
        message.error(error);
      }
    },
  },
};
